/**
 * File Name   : MemberCfgAction.java
 * Description : 
 */
package com.hh.admin.members.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.members.form.MemberCfgForm;
import com.hh.admin.members.form.ResultMemberBean;
import com.hh.admin.members.service.MembersCfgService;

/**
 * 会员信息管理
 * @author 冯昌硕
 * @create 2012-12-24 下午9:14:49
 */
public class MemberCfgAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(MemberCfgAction.class);
    
    private MembersCfgService memberCfgService;
    
    /**
     * 获取会员用户信息列表
     * @create 2012-12-24 下午9:21:04
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MemberCfgForm memberCfgForm = (MemberCfgForm) form;
        
        // 查询当前页要显示的记录
        List<ResultMemberBean> membersLst = memberCfgService.getMembersList(memberCfgForm);
        request.setAttribute("memberLst", membersLst);
        
        // 查询总记录数
        int totleCnt = memberCfgService.getMemberTotleCnt(memberCfgForm);
        memberCfgForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / memberCfgForm.getPageSize();
        if(totleCnt % memberCfgForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        memberCfgForm.setTotlePage(pageCnt);
        log.debug("查询会员用户记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("view");
    }
    
    /**
     * 删除单个指定会员用户记录
     * @create 2012-12-25 下午7:19:47
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MemberCfgForm memberCfgForm = (MemberCfgForm) form;
        String account = memberCfgForm.getAccount();
        log.debug("会员用户删除，要删除的账号=[" + account + "]");
        int delCnt = memberCfgService.deleteOneMemberUser(account);
        log.debug("会员用户[" + account + "]删除，删除影响行数=[" + delCnt + "]");
        return mapping.findForward("view");
    }
    
    /**
     * 删除单个指定会员用户记录
     * @create 2012-12-25 下午7:19:47
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MemberCfgForm memberCfgForm = (MemberCfgForm) form;
        String[] accounts = memberCfgForm.getAccounts();
        
        int delCnt = memberCfgService.deleteManyMemberUser(accounts);
        log.debug("会员用户批量删除，删除影响行数=[" + delCnt + "]");
        return mapping.findForward("view");
    }
    
    /**
     * 查看会员详情
     * @create 2012-12-25 下午8:12:46
     */
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MemberCfgForm memberCfgForm = (MemberCfgForm) form;
        String account = memberCfgForm.getAccount();
        
        ResultMemberBean resultBean = memberCfgService.getDetail(account);
        request.setAttribute("resultBean", resultBean);
        return mapping.findForward("detail");
    }

    public void setMemberCfgService(MembersCfgService memberCfgService) {
        this.memberCfgService = memberCfgService;
    }
}
